package tacos;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import lombok.Data;
import org.hibernate.validator.constraints.CreditCardNumber;
//import org.springframework.data.annotation.Id;

import javax.persistence.*;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

@Data
@Entity
public class TacoOrder implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotBlank(message = "Delivery name is required")
    private String deliveryName;

    @NotBlank(message = "Street is required")
    private String deliveryStreet;

    @NotBlank(message = "City is required")
    private String deliveryCity;

    @NotBlank(message = "State is required")
    private String deliveryState;

    @NotBlank(message = "Zip code is required")
    private String deliveryZip;

    // @CreditCardNumber注解。这个注解声明该属性的值必须是合法的信用卡号，它要能通过Luhn算法的检查。
    @CreditCardNumber(message = "Not a valid credit card number")
    private String ccNumber;

    // 使用了@Pattern注解并为其提供了一个正则表达式，确保属性值符合预期的格式。
    @Pattern(regexp = "^(0[1-9]|1[0-2])/(\\d{2})$", message = "Must be in MM/YY format")
    private String ccExpiration;

    @Digits(integer = 3, fraction = 0, message = "Invalid CVV")
    private String ccCVV;

    // TacoOrder所需的变更与Taco几乎如出一辙。但是，有一件事情需要特别注意，那就是我们在它与Taco对象列表的关系上使用了@OneToMany注解，表明这些taco都属于这一个订单。
    // 除此之外，cascade属性设置成了CascadeType.ALL，因此在删除订单的时候，所有关联的taco也都会被删除。
    @OneToMany(cascade = CascadeType.ALL)
    private List<Taco> tacos = new ArrayList<>();

    private Date placedAt;

    // 为了在TacoOrder实体和User实体之间实现所需的关联，需要为TacoOrder类添加一个新的属性：
    @ManyToOne
    private User user;

    public void addTaco(Taco taco) {
        this.tacos.add(taco);
    }
}
